package cloudspace.vm.javassist;

import java.util.List;

import javassist.CannotCompileException;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.expr.Expr;

/**
 * The Class Replace. This is the most simple and most widely supported command.
 * this class allows for special handling of certain types of methods and
 * expressions that may not be supported by javassist translations.
 * 
 * translate Replace <Expression Type> <Method signature> <Replacement body>
 * 
 */
public class Replace extends AbstractCommand
{
	/**
	 * Instantiates a new replace command.
	 * 
	 * @param args
	 *            the args
	 * 
	 * @throws MalformedCommandException
	 *             the malformed command exception
	 */
	public Replace(List<String> args) throws MalformedCommandException
	{
		super(args);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cloudspace.vm.javassist.commands.CommandInterface#translate
	 * (javassist.expr.Expr, java.lang.String)
	 */
	public void translate(Expr call, boolean isSuper)
			throws MalformedCommandException, CannotCompileException
	{
		String signature = getSignature();
		try
		{
			String repl = getReplacement();
			call.replace(repl);
		}
		catch (CannotCompileException e)
		{
			throw new MalformedCommandException(e.getMessage() + ": "
					+ getReplacement());
		}
		catch (RuntimeException e)
		{
			throw new CommandNotImplementedException(this);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cloudspace.vm.javassist.commands.CommandInterface#translate
	 * (javassist.CtMethod, java.lang.String)
	 */
	@Override
	public void translate(CtMethod member, String expressionName)
			throws MalformedCommandException, CannotCompileException
	{
		throw new CommandNotImplementedException(this);

	}
}
